package com.yunhe.dao.impl;

import java.util.List;


import org.hibernate.Query;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.yunhe.dao.IOptionDao;

import com.yunhe.entity.Option;

/**
 * 投票选项Dao层实现
 * */
public class OptionDaoImpl extends HibernateDaoSupport implements IOptionDao {
	/**
	 * 条件查询集合
	 * */
	@Override
	public List<Option> searchOptions(Option option) {
		String hql = "from Option where 1 = 1";
		if(option.getSubject()!=null){
			hql+=" and subject =:subject";
		}
		if(option.getVo_option()!=null){
			hql+=" and vo_option =:vo_option";
		}
		if(option.getVo_order()!=null){
			hql+=" and vo_order =:vo_order";
		}
		//生成query结果
		Query query = this.getSession().createQuery(hql);
		//hql语句赋值
		query.setProperties(option);
		//返回list结果
		List<Option> list = query.list();
		if(list.size()==0){
			return null;
		}
		return list;
		//创建list返回对象
		/*List<Option> list = null;
		try {
			//创建session对象
			Session session = HibernateUtil.currentSession();
			//书写hql语句
			String hql = "from Option where 1 = 1";
			if(option.getSubject()!=null){
				hql+=" and subject =:subject";
			}
			if(option.getVo_option()!=null){
				hql+=" and vo_option =:vo_option";
			}
			if(option.getVo_order()!=null){
				hql+=" and vo_order =:vo_order";
			}
			//生成query结果
			Query query = session.createQuery(hql);
			//hql语句赋值
			query.setProperties(option);
			//返回list结果
			list = query.list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		return list;*/
	}
	/**
	 * 条件查询对象
	 * */
	@Override
	public Option searchOption(Integer id) {
		String hql = "from Option where vo_id = "+id;
		List<Option> list = (List<Option>) this.getHibernateTemplate().find(hql);
		if(list.size()>0){
			return list.get(0);
		}
		return null;
		//创建返回对象
		/*Option option = null;
		try {
			//创建session对象
			Session session = HibernateUtil.currentSession();
			//书写hql语句
			String hql = "from Option where vo_id = "+id;
			
			//生成query结果
			Query query = session.createQuery(hql);
			
			//返回结果
			option = (Option)query.uniqueResult();
		} catch (HibernateException e) {
			e.printStackTrace();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		return option;*/
	}
	/**
	 * 增加
	 * */
	@Override
	public void addOption(Option option) {
		this.getHibernateTemplate().save(option);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行添加方法
			session.save(option);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}*/
		
	}
	/**
	 * 删除
	 * */
	@Override
	public void delOption(Option option) {
		this.getHibernateTemplate().delete(option);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.delete(option);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}*/
		
		
	}
	/**
	 * 修改
	 * */
	@Override
	public void updateOption(Option option) {
		this.getHibernateTemplate().update(option);
		/*Session session = null;
		try {
			//创建session对象
			session = HibernateUtil.currentSession();
			//开启transaction
			session.beginTransaction();
			//执行方法
			session.update(option);
			//提交
			session.getTransaction().commit();
			
		} catch (HibernateException e) {
			
			e.printStackTrace();
			//错误回滚
			session.getTransaction().rollback();
		}finally{
			//关闭session
			HibernateUtil.closeSession();
		}
		*/
		
	}

}
